<template>
<el-button type='primary' @click="dialogFormVisible = true">添加广告</el-button>
  <el-table :data="filterTableData" style="width: 100%">
    <el-table-column label="广告名称">
      <template #default="scope" >
          <el-popover
    placement="bottom-start"
  
    :width="200"
    :height="100"
    trigger="hover"
   
  >
  <div>
    <video :src="scope.row.path"  controls ></video>
  </div>
    <template #reference>
      {{scope.row.name}}
    </template>
  </el-popover>
       
        
      </template>
    </el-table-column>
    <el-table-column label="广告类型" prop="type" />
    <el-table-column prop="done" label="广告状态" width="100">
      <template #default="scope">
        <el-tag
          :type="!scope.row.done ? '' : 'success'"
          disable-transitions
        >{{ scope.row.done?'上线':'下线' }}</el-tag>
      </template>
    </el-table-column>
    <el-table-column align="right">
      <template #header>
        <el-input v-model="search" size="small" placeholder="Type to search" />
      </template>
      <template #default="scope">
  
<el-button size="small" @click="handleEdit(scope.row)" :style="{'background':scope.row.done?'rgb(242,126,39)':'green','color':'white'}">
  {{scope.row.done?'下线':'上线'}}
</el-button>
        
        <el-button size="small" type="danger" @click="handleDelete(scope.row._id)">Delete</el-button>
      </template>
    </el-table-column>
  </el-table>

  <!-- 添加表单 -->
  <el-dialog v-model="dialogFormVisible" title="Shipping address">
    <el-form :model="form">
      <el-form-item label="标题" :label-width="formLabelWidth">
        <el-input v-model="form.name" autocomplete="off" />
      </el-form-item>
     <el-form-item label="视频" :label-width="formLabelWidth">
         <el-upload
    class="avatar-uploader"
    action="http://localhost:3000/hcc/doRegister"
    :show-file-list="false"
    :on-success="handleAvatarSuccess"
    :on-exceed="exceed"
  >
    <video v-if="form.path" :src="form.path" class="avatar" controls />
    <el-icon  class="avatar-uploader-icon"><Plus /></el-icon>
  </el-upload>
     </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="qu">Cancel</el-button>
        <el-button type="primary" @click="sub">
          Confirm
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { computed, onMounted, ref, reactive } from "vue";
import axios from "axios";
import { ElMessage } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'

import type { UploadProps } from 'element-plus'
import func from "../../../vue-temp/vue-editor-bridge";
interface User {
  _id: any;
  name: String;
  path: String;
  type: String;
  done: Boolean;
}
const search = ref("");
const dialogFormVisible = ref(false)
const formLabelWidth = '140px'
const form = reactive({
  title:"",
  path:"",
  type:"积分",
  done:false,

})
const filterTableData = computed(() =>
  tableData.value.filter(
    (data) =>
      !search.value ||
      data.name.toLowerCase().includes(search.value.toLowerCase())
  )
);
async function handleEdit(row: User){
 await axios.post("http://localhost:3000/hcc/vedioEditDone",row);
  show()
};
async function handleDelete(index: number) {
   await axios.delete("http://localhost:3000/hcc/vedioDel/"+index);
  show()
};
const tableData: User[] = ref([]);
async function show() {
  let { data } = await axios.get("http://localhost:3000/hcc/vedioFind1");
  console.log(data, "data");
  if (data.code == 200) {
    tableData.value = data.data;
  }
}

onMounted(() => {
  show();
});
const handleAvatarSuccess: UploadProps['onSuccess'] = (
  response,
  uploadFile
) => {
        const fileExtension = response.path
        const regex = /http.*\.(jpg|gif|png|mp4)/;
				const matchResult = fileExtension.match(regex);
				if (matchResult) {
          console.log(matchResult[0].replace(/\\\\/g, '/'), 'object')
          let path = matchResult[0].replace(/\\\\/g, '/')
				  form.path = path; // 进行斜杆符号的替换操作
				} else {
				  console.log('文件路径不合法');
        }
        console.log(form,'11111') 
 
}
async function sub(){
  await axios.post("http://localhost:3000/hcc/vedioAdd",form);
  dialogFormVisible.value = false
  show()
}
 function exceed() {
        //重复选择文件,弹出错误弹窗
        this.$alert("最多仅支持上传一个视频文件")
    }
    function qu(){
       dialogFormVisible.value = false
       form.title = '',
       form.path =''
    }
</script>


<style lang="scss" scoped>
.avatar-uploader {
  width: 100px;
  height: 100px;
  border: 1px solid black;
 position: relative;
  .avatar{
    position: absolute;
     left: 122%;
     top: -26%;
    width:200px;
    height: 150px;
     line-height: 100px;
  }
   .avatar-uploader-icon{
     position: absolute;
     left: 27%;
     top: 27%;
     font-size: 45px;
  }
  
}
</style>